#include <stdio.h>
#include <iostream>

#include "MySocket.h"
#include "MyEventLoopThreadPool.h"

#include <csignal>

#if 0
http 短链接
1. 浏览器与server建立tcp
2. 浏览器向服务器要东西（Get）
3. 服务器给浏览器想要的东西
4. 浏览器与server断开连接
下次
goto 1

#endif
static  int keepRunning = 1;

void sig_handler( int sig )
{
    if ( sig == SIGINT)
    {
        keepRunning = 0;
    }
}



int main(int argc, char * argv[])
{
    printf("hello world!\n");
    //    if(argc != 2)
    //    {
    //        std::cout << "Usage: " << argv[0] << " <port>" << std::endl;
    //        return 0;
    //    }

    // 不要忘记在主线程中注册这个信号！！！
    signal( SIGINT, sig_handler );

    int port = 0;
    //int port = std::atoi(argv[1]);

    port = 8080;

    MySocket* _my_socket = new MySocket();


    _my_socket->Initialization(port);


    MyEventLoopThreadPool *threadPool = new MyEventLoopThreadPool(4);


    while(keepRunning)
    {
        int connFd = _my_socket->Accept();

        // 挑选一个线程，将已连接套接字注册到此线程的EventLoop中
        MyEventLoopThread *thread = threadPool->getNextThread();
        MyEventLoop *loop = thread->getLoop();
        loop->addToLoop(connFd);
    }

    return 0;
}
